import O1HD from "../../engines/gameEngine";
import btButton from "../control/btButton";

const {ccclass, property} = cc._decorator;

@ccclass
export default class logScreen extends cc.Component {
    // LIFE-CYCLE CALLBACKS:

    @property(cc.Node)
    ndUiLayer: cc.Node = null;
    @property(btButton)
    btShowHide: btButton = null;
    @property(cc.Node)
    ndList: cc.Node = null;
    @property(cc.Node)
    ndText: cc.Node = null;
    @property(cc.Slider)
    sOpacity: cc.Slider = null;
    @property()
    maxCount: number = 1000;

    //
    addLog(text: string, colorLoop: cc.Color) {
        //
        let str = text;
        let newText = cc.instantiate(this.ndText);
        this.ndList.addChild(newText);
        newText.color = colorLoop;
        newText.getComponent(cc.Label).string = str;
        //
        if (this.ndList.childrenCount > this.maxCount) {
            for (let i = 0; i < this.maxCount / 2; i++) {
                this.ndList.children[i].destroy();
            }
        }
        this.m_newFps = true;
        //
        this.unscheduleAllCallbacks();
        this.scheduleOnce(() => {
            this.onScrollDown();
        })
    }

    onLoad() {

    }

    protected start() {
        cc.game.addPersistRootNode(this.node);
    }

    private m_newFps: boolean = false;

    protected onScrollUp(isOnce?: boolean) {
        if (this.ndList.height < this.node.height) return;
        //
        let top = this.node.height / 2;
        //
        if (isOnce) {
            top = Math.max(top, this.ndList.y - this.node.height * 0.15);
        }
        //
        cc.tween(this.ndList)
            .to(0.5, {y: top}, {easing: 'cubicOut'/*'cubicOut'*/})
            .start();
    }

    protected onScrollDown(isOnce?: boolean) {
        if (this.ndList.height < this.node.height) return;
        //
        let top = this.node.height / 2;
        let down = top + (this.ndList.height - this.node.height);
        //
        if (isOnce) {
            down = Math.min(down, this.ndList.y + this.node.height * 0.15)
        }
        //
        cc.tween(this.ndList)
            .to(0.5, {y: down}, {easing: 'cubicOut'})
            .start();
        //
    }

    protected onBtnUp() {
        if (this.ndList.height < this.node.height) return;
        this.onScrollUp(true);
    }

    protected onBtnDown() {
        if (this.ndList.height < this.node.height) return;
        this.onScrollDown(true);
    }

    protected onBtnShowHide() {
        this.ndList.active = !this.ndList.active;
        this.ndUiLayer.active = !this.ndUiLayer.active;
        this.btShowHide.setButtonText(this.ndList.active ? '隐' : '显');
        if (this.ndList.active) {
            this.onScrollDown();
        }
    }

    protected onBtnClear() {
        this.ndList.destroyAllChildren();
    }

    protected onSetOpacity() {
        this.ndList.opacity = 20 + this.sOpacity.progress * (255 - 20);
    }

    update(dt) {
        if (this.m_newFps) {
            this.onScrollDown();
        }
        this.m_newFps = false;
    }
}
